package com.hzt.agprosaleplatform.controller;

import com.hzt.agprosaleplatform.common.Result;
import com.hzt.agprosaleplatform.common.StatusCode;
import com.hzt.agprosaleplatform.entity.User;
import com.hzt.agprosaleplatform.service.RegisterLoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 注册登录模块
 */
@RestController
@RequestMapping("/user")
@CrossOrigin(origins = "http://localhost:5173")
public class RegisterLoginController {
    @Autowired
    RegisterLoginService registerLoginService;
    
    // 存储短信验证码（生产环境建议使用Redis）
    private Map<String, String> smsCodeMap = new ConcurrentHashMap<>();
    private Map<String, Long> smsCodeTimeMap = new ConcurrentHashMap<>();
    
    // 验证码有效时间（5分钟）
    private static final long CODE_EXPIRE_TIME = 5 * 60 * 1000;

    //通过uid查询账户
    @GetMapping("/{uid}")
    public Result<User> selectByUid(@PathVariable("uid") String uid){
        User user = registerLoginService.selectUserByUid(uid);
        return new Result<User>(true, StatusCode.OK,"查询成功",user);
    }


    @PostMapping("/login")
    public Result<User> loginTest(@RequestBody User user){
        User loginuser = registerLoginService.selectUserByUid(user.getUid());

        if(loginuser == null){
            return new Result<>(false,StatusCode.ERROR,"Fail to Logining");
        }

        if(user.getPassword().equals(loginuser.getPassword())){
            return new Result<>(true, StatusCode.OK, "登录成功", loginuser);
        }else {
            return new Result<>(false, StatusCode.ERROR, "密码错误");
        }
    }

    // 短信验证码登录
    @PostMapping("/login/sms")
    public Result<User> loginBySMS(@RequestBody Map<String, String> loginData) {
        String phone = loginData.get("phone");
        String code = loginData.get("code");
        
        if (phone == null || phone.trim().isEmpty()) {
            return new Result<>(false, StatusCode.ERROR, "手机号不能为空");
        }
        
        if (code == null || code.trim().isEmpty()) {
            return new Result<>(false, StatusCode.ERROR, "验证码不能为空");
        }
        
        // 固定验证码为1234
        String fixedCode = "1234";
        
        // 验证验证码
        if (!fixedCode.equals(code)) {
            return new Result<>(false, StatusCode.ERROR, "验证码错误，请输入1234");
        }
        
        // 验证码正确，查找用户
        User user = registerLoginService.selectUserByPhone(phone);
        if (user == null) {
            return new Result<>(false, StatusCode.ERROR, "该手机号未注册");
        }
        
        return new Result<>(true, StatusCode.OK, "登录成功", user);
    }
    
    // 发送短信验证码
    @PostMapping("/sms/send")
    public Result<String> sendSMS(@RequestBody Map<String, String> requestData) {
        String phone = requestData.get("phone");
        
        if (phone == null || phone.trim().isEmpty()) {
            return new Result<>(false, StatusCode.ERROR, "手机号不能为空");
        }
        
        // 固定验证码为1234
        String fixedCode = "1234";
        
        // 在控制台输出验证码
        System.out.println("短信验证码：手机号 " + phone + " 的验证码是：" + fixedCode);
        
        return new Result<>(true, StatusCode.OK, "验证码发送成功", "验证码已发送到控制台（固定为1234）");
    }


    @PostMapping("/register")
    public Result<User> register(@RequestBody User user){
        User existingUser = registerLoginService.selectUserByUid(user.getUid());
        if (existingUser != null) {
            return new Result<>(false, StatusCode.ERROR, "该账户已存在！");
        } else {
            registerLoginService.addUser(user);
            return new Result<>(true, StatusCode.OK, "注册成功", user);
        }
    }


}
